import 'package:flutter/material.dart';

enum ActionWorld {
  sendGoods,
  historyBtn,
  changePounds,
}

newAppBar(
  BuildContext context,
  String title,
  Color backgroundColor,
  Color backIconColor,
  bool showBack,
  String iconPath, {
  double fontSize = 16, // 标题文字大小
  Function? goBack,
  Function? actionAction,
  ActionWorld? actionWorld,
  required String titleWorld,
  Color titleColor = Colors.black,
  Color iconColor = Colors.black,
}) {
  void back() async {
    if (goBack != null) {
      goBack();
    } else {
      Navigator.pop(context);
    }
  }

  void actionMoth() {
    if (actionAction != null) {
      actionAction();
    } else {}
  }

  List<Widget> setNewAction() {
    if (actionWorld != null) {
      if (actionWorld == ActionWorld.sendGoods) {
        return [
          InkWell(
              child: Container(
                padding: const EdgeInsets.all(15),
                child: Text(
                  titleWorld,
                  style: const TextStyle(color: Colors.black),
                ),
              ),
              onTap: actionMoth)
        ];
      } else if (actionWorld == ActionWorld.historyBtn) {
        return [
          InkWell(
            child: Container(
              padding: const EdgeInsets.only(top: 16, right: 21),
              child: Text(
                titleWorld,
                style: const TextStyle(color: Colors.black),
              ),
            ),
            onTap: actionMoth,
          )
        ];
      } else if (actionWorld == ActionWorld.changePounds) {
        return [
          InkWell(
            child: Container(
              padding: const EdgeInsets.only(top: 16, right: 21),
              child: Text(
                titleWorld,
                style: const TextStyle(color: Colors.black),
              ),
            ),
            onTap: actionMoth,
          )
        ];
      } else {
        return [];
      }
    }
    return [];
  }

  return AppBar(
    title: Text(
      title,
      style: TextStyle(
        fontSize: fontSize,
        color: titleColor,
        fontWeight: FontWeight.w600,
      ),
    ),
    elevation: 0,
    centerTitle: true,
    backgroundColor: backgroundColor,
    leading: showBack
        ? GestureDetector(
            onTap: back,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 14),
              child: iconPath == ""
                  ? Icon(Icons.arrow_back_ios, color: iconColor)
                  : Image.asset(
                      iconPath,
                      width: 22,
                      height: 22,
                      color: iconColor,
                    ),
            ),
          )
        : Container(),
    actions: setNewAction(),
  );
}
